<template>
  <div class="">
    <!-- {{ commandLine }}
    <button @click="activeExe" style="width: 100px;height: 40px;">启动EXE，并传值{{ stdouts }}    </button> -->
    <!-- <Cupdate :setNewData="setNewData"></Cupdate> -->
    <!-- {{ apiUrl.VUE_APP_HOST }}52555 -->
    <div v-if="!setNewData.error">
      <div v-if="setNewData.closeOpens">
        <Cupdate :setNewData="setNewData"></Cupdate>
      </div>
      <div v-if="!setNewData.closeOpens">
        <MainInterface :setNewData="setNewData" @refreshPage="refreshPage" :key="refreshIndex"></MainInterface>
      </div>
    </div>
    <div v-if="setNewData.error" class="netWork">
      <NetWork></NetWork>
    </div>
  </div>
</template>

<script setup>
import Vue from "vue";
import { ref, onMounted, onBeforeMount } from "vue";
import Cupdate from "../../components/updateLoading";
import MainInterface from "../../components/mainInterface";
import LandingPage from "../../components/LandingPage.vue";
import NetWork from "../404.vue";


import { Message, Loading } from "element-ui";
import { ipcRenderer } from "electron";
const { execFile } = require("child_process");
const path = require("path");
const fs = require("fs");
const unzipper = require("unzipper");
const apiUrl = process
console.log(apiUrl)
const stdouts=ref('没有值')

let setNewData = ref({}); //关闭更新页面，显示主界面

let filePath = ref("");
let progressStaus = ref(false);
let upperDirPath = ref(""); //替换的路径
let refreshIndex = ref(0)
const commandLine=ref('什么也没有')//当另一个实例请求启动应用程序时触发 

onMounted(() => {
  // 需要用到的数据
  ipcRenderer.on("setNewData", (event, data) => {
    console.log(data, "a主进程传过来的值打印")
    if(data.newRes){
      localStorage.setItem("token", data.newRes.access_token);
      localStorage.setItem("userInfo", JSON.stringify(data.newRes));
    }
    setNewData.value = data;
    // gainUpdate();
  });
  ipcRenderer.on("active", (event, data) => {
    commandLine.value=data
  })





});
onBeforeMount(() => {
  setNewData.value={
    show:true
  }
  window.addEventListener("popstate", handlePopState);
});

const handlePopState = (event) => {
  
  console.log("我返回执行主进程传过来的值打印");
  // console.log(setNewData.value , "有上一个页面返回");
  // 当有上一个页面返回时，可以在这里处理逻辑
};

const gainUpdate = () => {
  var exePath = process.execPath; //运行的exe位置
  upperDirPath.value = path.dirname(path.dirname(exePath)); //上一级文件位置:
  var launchExe = path.join(upperDirPath.value, "\\win-unpacked");
  // 不需要更新===》直接进入主页面==》检测启动的目录上级需要替换
  fs.access(launchExe, fs.constants.F_OK, (err) => {
    if (err) {
      // 无不替换
    } else {
      // 有替换
      detectionUpdate();
    }
    // console.log(err, "有替换，无不替换");
  });

  // console.log(exePath, "不需要更新===》");
  // console.log(upperDirPath, "不需要更新===》");
  // console.log(launchExe, "不需要更新===》");

  const detectionUpdate = () => {
    let zip =
      "https://maiding.jszyg.com:36020/smart-school-000000/win-unpacked.zip";
    ipcRenderer.invoke("start-download", { downloadUrL: zip });
  };
};

// 解压

const decompression = (zipPath) => {
  // console.log(zipPath, "下载完成需要解压-----");
  fs.createReadStream(zipPath)
    .pipe(unzipper.Extract({ path: upperDirPath.value }))
    .on("close", () => { });
};
const refreshPage = (item) => {
  refreshIndex.value += 1
  console.log(item)
}
// 启动
const activeExe=()=>{
  const exePath =`C:\\Program Files\\VTE\\VTE.exe`
  // C:\Users\11963\AppData\Local\Programs\electron-vue-admin

// 启动exe，并传递参数
const args = ['age1','age2'];

execFile(exePath, args, (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  stdouts.value=stdout
  // 输出exe返回的值
  console.log(error, stdout, stderr);
});


}
</script>

<style>
html,
body {
  background: transparent;
}
</style>
